package com.gzhryc.shared_device.oem.code.agent.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentTypeDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentType;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentTypeSearch;

public class AgentTypeService {

    static Logger log = Logger.getLogger(AgentTypeService.class);

    public static AgentTypeService self() {
        return MultiDBTools.getService(AgentTypeService.class, true);
    }

    public static AgentTypeService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentTypeService.class, true);
    }

    AgentTypeDao dao;

    public AgentTypeService(String jdbcKey) {
        this.dao = new AgentTypeDao(jdbcKey);
    }

    public List<AgentType> findBySearch(AgentTypeSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findByCondition(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public List<AgentType> findAll(){
        try {
            return dao.findAll();
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public AgentType getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public AgentType getByName(String name) {
    	if(StringTools.isNotBlank(name)) {
    		Conditions condition = new Conditions(AgentType.class);
    		condition.and().is("name", name);
    		
    		try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }

    public synchronized boolean add(AgentType agentType) throws LogicException {
    	AgentType temp = getByName(agentType.getName());
        if(temp != null){
            throw new LogicException("名称已存在");
        }
        
        agentType.setCreateDate(new Date());
        try {
            return dao.insert(agentType) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    public synchronized boolean updateNotNull(AgentType agentType) throws LogicException {
        try {
        	AgentType temp = getByName(agentType.getName());
            if(temp != null && !temp.getId().equals(agentType.getId())){
                throw new LogicException("名称已存在");
            }
        	
            return dao.updateNotNull(agentType) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        JdbcSession.begin();
        try {
            if(dao.deleteById(id) > 0){
                if(AgentTypeRelService.self().deleteByTypeId(id)) {
                	if(AgentTypeMenuRelService.self().deleteByTypeId(id)) {
                		JdbcSession.commit();
                		return true;
                	}
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        } finally {
            JdbcSession.end();
        }
        return false;
    }
}
